var express="申通快递";
var total=0;
var zongjia=0;
var addressId;
var pay="支付宝付款";
function BuyCar_Three(){
    if (addressId == undefined) {
        alert("请先选择收货地址！");
    }else{
        localStorage.setItem("express",express);
        localStorage.setItem("pay",pay);
        localStorage.setItem("zongjia",zongjia);
        var user=JSON.parse(sessionStorage.getItem("user"));
        $.ajax({
            url:"easybuy/addOrder",
            async:false,
            type:"post",
            dataType:"text",
            data:{
                userId:user.id,
                addressId:addressId,
                zongjia:zongjia
            },
            success:function (serialNumber) {
                if (serialNumber!=null||serialNumber!='') {
                    alert("下单成功！");
                    localStorage.setItem("serialNumber",serialNumber);
                    location.href="BuyCar_Three.html";
                }else {
                    alert("下单失败！");
                }
            },
            error:function (data) {
                alert("出现错误:"+data);
            }
        })
    }

}
$(function () {
    var user=JSON.parse(sessionStorage.getItem("user"));
    $.ajax({
        url:"easybuy/shopping",
        dataType:"json",
        data:{
            userId:user.id
        },
        success:function (shopList) {
            for (var i in shopList){
                var background="";
                if (i%2==1){
                    background="class='car_tr'";
                }
                total=total+shopList[i].cost;
                $("#tr1").after("<tr "+background+">\n" +
                    "                <td>\n" +
                    "                    <div class=\"c_s_img\"><img src='images/"+shopList[i].fileName+"' width=\"73\" height=\"73\" /></div>\n" +
                    shopList[i].name +
                    "                </td>\n" +
                    "                <td align=\"center\">"+shopList[i].price+"</td>\n" +
                    "                <td align=\"center\">"+shopList[i].quantity+"</td>\n" +
                    "                <td align=\"center\" style=\"color:#ff4e00;\">￥"+shopList[i].cost+"</td>\n" +
                    "              </tr>")
            }
            $("[name='total']").html("￥"+total);
            zongjia=total+15;
            $("#totall").html("￥"+zongjia);
        }

    });
    $.ajax({
        url:"easybuy/getUserAddress",
        dataType: "json",
        async:false,
        data:{
            userId:user.id
        },
        success:function (addressList) {
            if (addressList==null||addressList==''){
                $("#changeAddress").hide();
            }else{
                $("#addressName").html(addressList[0].userName);
                $("#addressPhone").html(addressList[0].mobile);
                $("#addressMess").html(addressList[0].address);
                $("#addressRemark").html(addressList[0].remark);
                addressId=addressList[0].id;
                var table=$(".changeAddress table");
                for (var i in addressList) {
                    var check="";
                    if (i==0){
                        check="checked";
                    }
                    $(table).append("<tr'>\n" +
                        "                            <td width=\"40%\" style=\"background-color: #eaeaea\">"+addressList[i].remark+"</td>\n" +
                        "                            <td width=\"60%\">"+addressList[i].address+"</td>\n" +
                        "                            <td width=\"10%\"><input name='addressCheck' value='"+addressList[i].id+"' type=\"radio\" "+check+"></td>\n" +
                        "                        </tr>")
                }
            }

        }
    });
    $(".changeAddress input").change(function () {
        var id=$(this).val();
        addressId=id;
        $.ajax({
            url:"easybuy/getAddress",
            dataType:"json",
            data:{
                id:id
            },
            success:function (address) {
                $("#addressName").html(address.userName);
                $("#addressPhone").html(address.mobile);
                $("#addressMess").html(address.address);
                $("#addressRemark").html(address.remark);
            }
        })
    });
    $("#changeAddress").hover(function () {
        $(".changeAddress").show();
    },function () {
        $(".changeAddress").hide();
    });
    $("[name='ch']").click(function () {
        express=$(this).val();
    });
    $(".pay li").click(function () {
        pay=$(this).text();
        $(this).siblings("li").removeClass("checked");
        $(this).addClass("checked");
    });
})